/*
 * Copyright 2009 Christian Schindelhauer, Peter Thiemann, Faisal Aslam, Luminous Fennell and Gidon Ernst.
 * All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 3 for more details (a copy is
 * included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU General Public License
 * version 3 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Please contact Faisal Aslam
 * (aslam AT informatik.uni-freibug.de or studentresearcher AT gmail.com)
 * if you need additional information or have any questions.
 */
package takatuka.offlineGC.generateInstrs;

import takatuka.classreader.dataObjs.MethodInfo;
import takatuka.classreader.logic.logAndStats.*;
import takatuka.offlineGC.DFA.dataObjs.attribute.*;
import takatuka.optimizer.cpGlobalization.logic.util.Oracle;
import takatuka.vm.autoGenerated.forExceptionPrettyPrint.*;

/**
 * <p>Title: </p>
 * <p>Description:
 *
 * </p>
 * @author Faisal Aslam
 * @version 1.0
 */
public class LogRecord implements Comparable<LogRecord> {

    private GCInstruction inst = null;
    private int newInstrId  = -1;
    private String algorithmName = "";
    public static final String LOG_FILE_FOR_OFFLINE_GC = "offLineGC.log";
    private Oracle oracle = Oracle.getInstanceOf();

    public LogRecord(GCInstruction instr, int newInstrId) {
        this.inst = instr;
        this.newInstrId = newInstrId;
    }

    public LogRecord(GCInstruction instr, int newInstrId, String algoName) {
        this(instr, newInstrId);
        this.algorithmName = algoName;
    }

    public int compareTo(LogRecord o) {
        
        int ret = new Integer(newInstrId).compareTo(o.newInstrId);
        if (ret == 0) {
            ret = new Long(inst.getInstructionId()).compareTo(o.inst.getInstructionId());
        }
        return ret;
    }

    public void addLog() {
        LogHolder.getInstanceOf().addLog(toString(), LOG_FILE_FOR_OFFLINE_GC, false);
    }

    @Override
    public String toString() {
        MethodInfo method = inst.getParentCodeAtt().getMethod();
        String methodStr = oracle.getMethodOrFieldString(method);

        return methodStr + ", line number ="
                + LineNumberController.getInstanceOf().getLineNumberInfo(inst.getInstructionId())
                + ", for new Id =" + newInstrId
                + ", adding request instr id =" + inst.getInstructionId() + ", "
                + inst.getMnemonic()+", "+algorithmName;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof LogRecord)) {
            return false;
        }
        LogRecord input = (LogRecord) obj;
        if (input.inst.getInstructionId() == inst.getInstructionId()
                && newInstrId == input.newInstrId) {
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.inst != null ? this.inst.hashCode() : 0);
        hash = 47 * hash + this.newInstrId;
        return hash;
    }

    
}
